﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using StockholmsStad.Windows.UI.Modules.Ceremony.Models;
using StockholmsStad.Utilities;



namespace StockholmsStad.Windows.UI.Modules.Ceremony.Controls
{
    /// <summary>
    /// Interaction logic for PrintSelector.xaml
    /// </summary>
    public partial class PrintSelector
    {
        public PrintSelector()
        {
            InitializeComponent();

            btnPrint.Click += btnPrint_Click;
            btnCancel.Click += btnCancel_Click;
        }

        public CeremonyDetailsPresentationModel Model { get; set; }

        void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            // Sätt credentials på URL'en till rapporterna (osäkert hur)
            // Dom får använda en gemensam användare så länge - lokal användare på rapport servern

            if (Model != null)
            {
                var link = new Hyperlink{TargetName = "_blank"};

                string strUri = SettingsUtility.GetAppSetting(Facade.Properties.Resources.ReportViewer);

                if (RadioButtonPrintAllCertificates.IsChecked.HasValue && RadioButtonPrintAllCertificates.IsChecked.Value)
                {
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.MarriageCertificateSummaryReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&CeremoniDate=" + Model.BookingDate.ToShortDateString();

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?%2fReports%2fMarriageCertificate&rs%3aCommand=Render&rc:Parameters=false&date=2009-02-16&coupleID=17");
                }
                if (RadioButtonPrintAllReports.IsChecked.HasValue && RadioButtonPrintAllReports.IsChecked.Value)
                {
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.MarriageRegistrationFormSummaryReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&CeremoniDate=" + Model.BookingDate.ToShortDateString();

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?%2fReports%2fMarriageCertificate&rs%3aCommand=Render&rc:Parameters=false&date=2009-02-16&coupleID=17");
                }
                if (RadioButtonPrintedMarriageCertificate.IsChecked.HasValue && RadioButtonPrintedMarriageCertificate.IsChecked.Value)
                {
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.MarriageCertificateReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&date=" + Model.BookingDate.ToShortDateString() +
                             "&coupleID=" + Model.CoupleId;

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?%2fReports%2fMarriageCertificate&rs%3aCommand=Render&rc:Parameters=false&date=2009-02-16&coupleID=17");
                }
                if (RadioButtonPrintedMarriageReport.IsChecked.HasValue && RadioButtonPrintedMarriageReport.IsChecked.Value)
                {
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.MarriageRegistrationFormReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&coupleId=" + Model.CoupleId;

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?/Reports/MarriageRegistrationForm&rs:Command=Render&rc:Parameters=false&coupleId=677");
                }
                if (RadioButtonPrintedMarriageRegister.IsChecked.HasValue && RadioButtonPrintedMarriageRegister.IsChecked.Value)
                {
                    strUri = strUri +
                             SettingsUtility.GetAppSetting(Facade.Properties.Resources.MarriageRegisterReportPath) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriCommand) +
                             "&" + SettingsUtility.GetAppSetting(Facade.Properties.Resources.UriParameters) +
                             "&date=" + Model.BookingDate.ToShortDateString();

                    link.NavigateUri = new Uri(strUri);

                    //link.NavigateUri = new Uri("http://parvis.input.nu/ReportServer/Pages/ReportViewer.aspx?/Reports/MarriageRegister&rs:Command=Render&rc:Parameters=false&date=6/6/2009&ministerId=1");
                }
           
                Process.Start(link.NavigateUri.AbsoluteUri);
            }

            Close();
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        public void Close()
        {
            var window = ((StackPanel) Parent).Parent as Window;

            if (window != null)
                window.Close();
        }

    }
}
